"""
    @Author  ：思念 
    @File    ：8.淘天招聘内容写入MySQL.py
    @Date    ：2024/12/13 20:27 
"""
import requests
import pymysql


class TtWork:
    def __init__(self):
        self.api_url = 'https://talent.taotian.com/position/search?_csrf=b19affe5-31dd-44d1-a473-118f5e9678c6'
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.54',
            'Cookie': 'XSRF-TOKEN=b19affe5-31dd-44d1-a473-118f5e9678c6; prefered-lang=zh; SESSION=M0M4NkIyRUJCMzhBMzFEQUE0MjE5OTc4MTI0QUYxQkQ=; cna=2RXjH+q9XzsBASQOA5qAKsCD; xlly_s=1; tfstk=ghhrxXvmEQdy5_cyxrVFgh2PMpV8O7x1ZXZQ-203PuquALLFTmugK698NyAEAmwnN7gS0DoZf8cWe4ght2qYAuZSFyAEmmwuOYZ7-ahqb61W2L3hTWNHfhO614379Wx_XViV9gU376ACZ6V0wyinbTDB143RoaSH5xA1YEZXd2qnx820nyq0r9Vnxsb0DyUhqyfkuqqYmzjuxk0ciy4PKMm3tEu0Dyzut8cxa0ndfP2kWU40BmWSY8riro5ZkjzllkOTD6qtwzery2E4lrG4z8rgH2STubmj-bG8HdB_9qMZYvmDg_oErAm3VjxGTkMT824asCQambuEg8HfqT4aauyiZ-b1Gq2n3jPSZHBtlqruQSMXH3wQa0kTX8vvDDu4VDczng-bAAgjZ-oMcsoLLYmzo5jPFMU0hQGpr2XUErU4fE8qI2SKDlnH4WWdp-2gulT8K9BLErU4fE8Vp9eRsrr6yJf..; isg=BPX1oRe0_ujORBpUwBD8bRgABHGvcqmE8Lh2iHcZ1mzWThdAP8PbVw6ImBL4NcE8'
        }
        self.db = pymysql.connect(
            host='localhost',
            port=3306,
            user='root',
            password='root',
            database='py_spider',
            charset='utf8'
        )
        self.cursor = self.db.cursor()

    def create_table(self):
        drop_table_sql = 'drop table if exists Tt_work'
        create_table_sql = '''
            create table if not exists Tt_work(
                id int primary key auto_increment,
                name varchar(100),
                requirement varchar(500),
                workLocation varchar(50),
                description text
            )
        '''
        try:
            self.cursor.execute(drop_table_sql)
            print('删除表成功')
            self.cursor.execute(create_table_sql)
            print('创建表成功')
        except Exception as e:
            print('创建表失败')
            print(e)

    def close_db(self):
        self.cursor.close()
        self.db.close()
        print('数据库关闭成功,爬虫结束')

    def insert_data(self, *args):
        insert_sql = 'insert into Tt_work(id,name,requirement,workLocation,description) values(%s,%s,%s,%s,%s)'
        try:
            self.cursor.execute(insert_sql, args)
            self.db.commit()
            print('插入数据成功')
        except Exception as e:
            print('插入数据失败')
            print(e)
            self.db.rollback()

    def main(self):
        self.create_table()
        json_data = {
            "batchId": "",
            "categories": "",
            "channel": "group_official_site",
            "deptCodes": [],
            "key": "",
            "language": "zh",
            "myReferralShareCode": "",
            "pageIndex": "",
            "pageSize": "",
            "regions": "",
            "shareId": "",
            "shareType": "",
            "subCategories": ""
        }
        for i in range(1, 10):
            json_data['pageIndex'] = str(i)  # 更新 pageIndex 的值
            response = requests.post(self.api_url, headers=self.headers, json=json_data)
            data = response.json()
            work_id = 0
            for item in data['content']['datas']:
                name = item['name']
                requirement = item['requirement']
                workLocation = ''
                for i in item['workLocations']:
                    workLocation = i
                description = item['description']
                self.insert_data(work_id, name, requirement, workLocation, description)
        self.close_db()


if __name__ == '__main__':
    tt_work = TtWork()
    tt_work.main()

    # api_url = 'https://talent.taotian.com/position/search?_csrf=b19affe5-31dd-44d1-a473-118f5e9678c6'
    # headers = {
    #     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.54',
    #     'Cookie': 'XSRF-TOKEN=b19affe5-31dd-44d1-a473-118f5e9678c6; prefered-lang=zh; SESSION=M0M4NkIyRUJCMzhBMzFEQUE0MjE5OTc4MTI0QUYxQkQ=; cna=2RXjH+q9XzsBASQOA5qAKsCD; xlly_s=1; tfstk=ghhrxXvmEQdy5_cyxrVFgh2PMpV8O7x1ZXZQ-203PuquALLFTmugK698NyAEAmwnN7gS0DoZf8cWe4ght2qYAuZSFyAEmmwuOYZ7-ahqb61W2L3hTWNHfhO614379Wx_XViV9gU376ACZ6V0wyinbTDB143RoaSH5xA1YEZXd2qnx820nyq0r9Vnxsb0DyUhqyfkuqqYmzjuxk0ciy4PKMm3tEu0Dyzut8cxa0ndfP2kWU40BmWSY8riro5ZkjzllkOTD6qtwzery2E4lrG4z8rgH2STubmj-bG8HdB_9qMZYvmDg_oErAm3VjxGTkMT824asCQambuEg8HfqT4aauyiZ-b1Gq2n3jPSZHBtlqruQSMXH3wQa0kTX8vvDDu4VDczng-bAAgjZ-oMcsoLLYmzo5jPFMU0hQGpr2XUErU4fE8qI2SKDlnH4WWdp-2gulT8K9BLErU4fE8Vp9eRsrr6yJf..; isg=BPX1oRe0_ujORBpUwBD8bRgABHGvcqmE8Lh2iHcZ1mzWThdAP8PbVw6ImBL4NcE8'
    # }
    # json_data = {
    #     "batchId": "",
    #     "categories": "",
    #     "channel": "group_official_site",
    #     "deptCodes": [],
    #     "key": "",
    #     "language": "zh",
    #     "myReferralShareCode": "",
    #     "pageIndex": "",
    #     "pageSize": "",
    #     "regions": "",
    #     "shareId": "",
    #     "shareType": "",
    #     "subCategories": ""
    # }
    #
    # for i in range(1, 10):
    #     json_data['pageIndex'] = str(i)  # 更新 pageIndex 的值
    #     response = requests.post(api_url, headers=headers, json=json_data)
    #     data = response.json()
    #     for item in data['content']['datas']:
    #         name = item['name']
    #         requirement = item['requirement']
    #         workLocation = ""
    #         for i in item['workLocations']:
    #             workLocation = i
    #         description = item['description']
    #         print(name, requirement, workLocation, description)
